package com.tencent.crazypenguin.bean;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.tencent.crazypenguin.util.Http;

import net.sf.json.JSONObject;

public class MatchList {
	
	private static Map<Long,MUser> machQueen = new HashMap<Long,MUser>();
	
	public static synchronized void addMuser(long userId,MUser mUser){
		machQueen.put(userId, mUser);
		for(long id:machQueen.keySet()){
			JSONObject json = JSONObject.fromObject(machQueen.get(id));
			System.out.println(json.toString());
		}
		
	}
	
	public static synchronized void RemoveMuser(long userId){
		machQueen.remove(userId);
	}
	
	public static synchronized List<MatchItem> getMusers(long userId,MUser mUser){
		test();
		List<MatchItem> mList = new ArrayList<MatchItem>();
		for(long id:machQueen.keySet()){
			if(id == userId){
				continue;
			}
			MUser mUser2 = machQueen.get(id);
			if(id==2){
				System.out.println(mUser2);
				System.out.println(mUser2.origin);
			}
			double dOrigin = getDistance(mUser.origin.latitude,mUser.origin.longitude,mUser2.origin.latitude,mUser2.origin.longitude,"walking");
			double dDestWalk = getDistance(mUser.target.latitude,mUser.target.longitude,mUser2.target.latitude,mUser2.target.longitude,"walking");
			double dDestDrvie = getDistance(mUser.target.latitude,mUser.target.longitude,mUser2.target.latitude,mUser2.target.longitude,"driving");
			if(dOrigin<=1000){
				if(dDestWalk<=1000){
					mList.add(new MatchItem(id,new MUser(mUser2),dDestWalk,"walking"));
				}
				else if(dDestDrvie<=2000){
					mList.add(new MatchItem(id,new MUser(mUser2),dDestDrvie,"driving"));
				}
			}
		}
		
		mList.sort(new Compare());
		
		return mList;
	}
	
	public static double getDistance(double lat1, double lng1, double lat2,  
	        double lng2,String type) {  
		/** 
		 * 计算地球上任意两点(经纬度)距离 
		 *  
		 * @param lat1 
		 *            第一点纬度 
		 * @param lng1 
		 *            第一点经度
		 * @param lat2 
		 *            第二点纬度
		 * @param lng2 
		 *            第二点经度
		 * @return 返回距离 单位：米 
		 */  
	    JSONObject json;
		try {
			Map<String,String> params = new HashMap<String,String>();
			params.put("origins", lat1+","+lng1);
			params.put("destinations", lat2+","+lng2);
			params.put("mode", type);
			params.put("output", "json");
			params.put("ak", "F9b8G66W1ajFR9kAdAKoPUceCj1qSzjg");
			
			String jsonStr = Http.sendGet("http://api.map.baidu.com/direction/v1/routematrix", params).getContent();
			json = JSONObject.fromObject(jsonStr);
			return json.getJSONObject("result").getJSONArray("elements").getJSONObject(0).getJSONObject("distance").getDouble("value");  

			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return 999999;
		
	}
	
	public static void test(){
		addMuser(1,new MUser(22.548298,114.062824,22.552278,114.050086));
		addMuser(3,new MUser(22.544852,114.06719,22.552295,114.051452));
		addMuser(4,new MUser(22.554732,114.077251,22.551894,114.053033));
		addMuser(5,new MUser(22.554231,114.07682,22.551278,114.05138));
		
//		List<MatchItem> list = getMusers(new MUser(22.547939,114.067082,22.552362,114.051524));
//		
//		for(Iterator<MatchItem> it = list.iterator();it.hasNext();){
//			MatchItem m = it.next();
//			System.out.println(m.userId+"  "+m.distance+"  "+m.Type);
//		}
	}
}
	

	class Compare implements Comparator<MatchItem>{

		@Override
		public int compare(MatchItem m1, MatchItem m2) {
			if(m1.distance>m2.distance)
				return 1;
			if(m1.distance==m2.distance)
				return 0;
			else
				return -1;
		}
		
	}



